layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 提总分拣单管理
     */
    var KhTzStorageOrder = {
        tableId: "khTzStorageOrderTable"
    };

    /**
     * 初始化表格的列
     */
    KhTzStorageOrder.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'orderNumber', sort: true, title: '单号'},
            {field: 'orderState', sort: true, title: '单据状态'},
            {field: 'goodsId', sort: true, title: '商品id'},
            {field: 'goodsName', sort: true, title: '商品名称'},
            {field: 'goodsNumber', sort: true, title: '商品编号'},
            {field: 'dismantlingParts', sort: true, title: '是否拆零'},
            {field: 'unit', sort: true, title: '单位'},
            {field: 'rate', sort: true, title: '换算率'},
            {field: 'psNum', sort: true, title: '数量'},
            {field: 'jibenNum', sort: true, title: '基本数量'},
            {field: 'setWarehouse', sort: true, title: '发货仓库'},
            {field: 'createUserName', sort: true, title: '创建人'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'completedTime', sort: true, title: '完成时间'},
            {field: 'storageUserName', sort: true, title: '分拣员'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    KhTzStorageOrder.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(KhTzStorageOrder.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {

            }
        });
    };

    /**
     * 导出excel按钮
     */
    KhTzStorageOrder.exportExcel = function () {
        var checkRows = table.checkStatus(KhTzStorageOrder.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + KhTzStorageOrder.tableId,
        url: Feng.ctxPath + '/khTzStorageOrder/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        height: 410,
        cols: KhTzStorageOrder.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        KhTzStorageOrder.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        KhTzStorageOrder.exportExcel();
    });


    /**
     * 提总分拣任务管理
     */
    var KhTzStorageOrderItem = {
        tableId: "khTzStorageOrderItemTable"
    };

    /**
     * 初始化表格的列
     */
    KhTzStorageOrderItem.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'goodsName', sort: true, title: '商品名称'},
            {field: 'goodsNumber', sort: true, title: '商品编号'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'unit', sort: true, title: '单位'},
            {field: 'rate', sort: true, title: '换算率'},
            {field: 'psNum', sort: true, title: '数量'},
            {field: 'dismantlingParts', sort: true, title: '是否拆零'},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'jibenNum', sort: true, title: '基本数量'},
            {field: 'goodsXcode69', sort: true, title: '商品码2'},
            {field: 'goodsHcode69', sort: true, title: '商品码3'},
            {field: 'positionNumber', sort: true, title: '库位编号'},
            {field: 'positionPickingorder', sort: true, title: '拣货顺序'},
            {field: 'completedTime', sort: true, title: '完成时间'},
            {field: 'storageUserName', sort: true, title: '分拣员'},
            {field: 'storageState', sort: true, title: '分拣状态'}
        ]];
    };

    // 单击行事件
    table.on('row(' + KhTzStorageOrder.tableId + ')', function (obj) {
        var data = obj.data;
        $('div[lay-id="' + KhTzStorageOrder.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
        $('div[lay-id="' + KhTzStorageOrder.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
        khTzStorageOrder(data);
    });

    function khTzStorageOrder(data){
        var orderNumber;
        if (data!=null) {
            orderNumber = data.orderNumber;
        } else {
            orderNumber = '0';
        }
        // 渲染商品明细表格
        var tableResult = table.render({
            elem: '#' + KhTzStorageOrderItem.tableId,
            url: Feng.ctxPath + '/khTzStorageOrderItem/list?orderNumber=' + orderNumber,
            page: true,
            limit: 1000,
            limits:[60,120,240,360,480,1000,2000,4000],
            height: 400,
            toolbar: '#toolbarfromDetailed',
            cellMinWidth: 100,
            totalRow: true ,//开启合计行
            cols: KhTzStorageOrderItem.initColumn()
        });

        /**
         * 点击查询按钮
         */
        KhTzStorageOrderItem.search = function () {
            var queryData = {};
            queryData['condition'] = $("#condition2").val();
            table.reload(KhTzStorageOrderItem.tableId, {
                where: queryData, page: {curr: 1},
                done:function () {
                    // 搜索按钮点击事件
                    $('#btnSearchDetailed').click(function () {
                        KhTzStorageOrderItem.search();
                    });
                }
            });
        };
        // 搜索按钮点击事件
        $('#btnSearchDetailed').click(function () {
            KhTzStorageOrderItem.search();
        });

    }

});
